package pomodorotasks.testutils

import griffon.core.GriffonApplication
import griffon.test.GriffonUnitTestCase

import com.google.api.services.tasks.model.TaskLists

import pomodorotasks.GoogleTasksService

/**
 *
 * @author Dawid Pytel
 */
class PomodoroApplicationFacade {
    
    GriffonApplication app
    
    GriffonUnitTestCase testCase
    
    def pomodoroTasksController
    def pomodoroTasksView
    def pomodoroTasksModel
    
    def mvcGroupsToClose = []
    
    public PomodoroApplicationFacade(GriffonApplication app, GriffonUnitTestCase testCase) {
        this.app = app
        this.testCase = testCase
    }
    
    public def mockGoogleTasksService() {
        def mockGoogleTasksService = testCase.mockFor(pomodorotasks.GoogleTasksService, false)
        mockGoogleTasksService.demand.initConnection(0..1) { }
        return mockGoogleTasksService
    }
    
    public void startApp() {
        app.startup()
        app.ready()
        app.realize()
        app.show()
        
        pomodoroTasksController = app.controllers['PomodoroTasks']
        pomodoroTasksView = app.views['PomodoroTasks']
        pomodoroTasksModel = app.models['PomodoroTasks']
    }
    
    public void clean() {
        mvcGroupsToClose.each {mvcGroupName -> app.destroyMVCGroup(mvcGroupName)}
        pomodoroTasksView.listsPane.removeAll()
        ThreadingUtils.waitForAsyncOperationsComplete() // make sure view is updated
    }
    
    public TaskLists addTaskLists(taskListsNames) {
        TaskLists taskLists = GoogleTasksUtils.createTaskList(taskListsNames)
        UIThreadManager.INSTANCE.executeSync {
            pomodoroTasksController.updateTaskList(taskLists)
        }
        ThreadingUtils.waitForAsyncOperationsComplete() // make sure view is updated
        return taskLists
    }
    
    public void selectTaskList(taskListName) {
        UIThreadManager.INSTANCE.executeSync {
            def listsComboBox = pomodoroTasksView.listsComboBox
            listsComboBox.itemCount.times { index ->
                if (listsComboBox.getItemAt(index) == taskListName) {
                    listsComboBox.setSelectedIndex(index)
                }
            }
        }
        mvcGroupsToClose.add(taskListName)
        ThreadingUtils.waitForAsyncOperationsComplete() // make sure view is updated
    }
    
    public def getPomodoroTasksListController(taskListName) {
        def controller = app.controllers[taskListName]
        assert controller != null : "Unavailable task list's name: $taskListName. Available: ${app.views.keySet()}"
        return controller
    }
    
    public def getPomodoroTasksListView(taskListName) {
        def view = app.views[taskListName]
        assert view != null : "Unavailable task list's name: $taskListName. Available: ${app.views.keySet()}"
        return view
    }
    
    public def getPomodoroTasksListModel(taskListName) {
        def model = app.models[taskListName]
        assert model != null : "Unavailable task list's name: $taskListName. Available: ${app.views.keySet()}"
        return model
    }
}

